package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.ICustomerServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 王若龙
 * @CreateTime 2022-09-22 13:11
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private ICustomerService iCustomerService = new ICustomerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name").trim();
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        try {
            Customer customer = iCustomerService.login(name, password);
            session.setAttribute("customer", customer);
            resp.sendRedirect("/index.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            session.setAttribute("error", e.getMessage());
            resp.sendRedirect("/login.jsp");
        }
    }
}
